<?php

namespace backend\modules\supplier\models;

use Yii;

/**
 * 入库单模型。
 *
 * @property integer $id
 * @property string $sn
 * @property integer $factory_id
 * @property string $factory_name
 * @property string $total_price
 * @property string $total_price_in
 * @property string $total_paid
 * @property integer $add_id
 * @property integer $add_time
 * @property integer $chk_id
 * @property integer $chk_date
 * @property integer $pay_id
 * @property integer $pay_date
 * @property integer $status
 * @property string $remark
 * @property integer $supplier_id
 */
class PurchaseIn extends \common\models\AR {

    public $modelName = '采购进货单';

    /**
     * 未审核
     */
    const STATUS_UNAUDIT = 0;

    /**
     * 已审核
     */
    const STATUS_AUDITED = 1;

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%s_purchase_in}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            //[['sn'], 'required'],
            [['factory_id', 'add_id', 'add_time', 'chk_id', 'chk_date', 'pay_id', 'pay_date', 'status', 'supplier_id'], 'integer'],
            [['total_price', 'total_price_in', 'total_paid'], 'number'],
            [['remark', 'add_name', 'chk_name', 'pay_name'], 'string'],
            [['status'], 'default', 'value' => self::STATUS_UNAUDIT],
            [['sn'], 'string', 'max' => 25],
            [['factory_name'], 'string', 'max' => 50]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'sn' => '编号',
            'factory_id' => '所属工厂',
            'factory_name' => '所属工厂名称',
            'total_price' => '采购单总金额',
            'total_price_in' => '已入库总金额',
            'total_paid' => '已支付总金额',
            'add_id' => '添加人ID',
            'add_time' => '添加时间',
            'add_name' => '添加人',
            'chk_id' => '审核人ID',
            'chk_date' => '审核时间',
            'chk_name' => '审核人员',
            'pay_id' => '支付人ID',
            'pay_date' => '付款时间',
            'pay_name' => '支付人员',
            'status' => '状态',
            'remark' => '备注',
            'supplier_id' => '供应商ID',
        ];
    }

    public function beforeSave($insert) {
        if (!parent::beforeSave($insert)) {
            return FALSE;
        }
        if ($this->isNewRecord) {
            $this->supplier_id = Yii::$app->user->identity->supplier_id;
            $this->add_time = time();
            $this->add_id = Yii::$app->user->identity->id;

            $this->sn = $this->createSn();
        }

        return true;
    }

    /**
     * 获取采购单详情
     * @return type
     */
    public function getDetail() {
        return $this->hasMany(PurchaseInDetail::className(), ['master_id' => 'id']);
    }

    /**
     * 获取和采购单关联的入库单列表
     */
    public function getStorageInList(){
        
    }
    
    /**
     * 生成采购单编号
     * @return string
     */
    public function createSn() {
        $supplierId = Yii::$app->user->identity->supplier_id;
        $last = PurchaseIn::find()->where(['supplier_id' => $supplierId])->orderBy(['id' => SORT_DESC])->one();

        $con = '-';
        $date = date('Ymd');
        if ($last) {
            $arr = explode($con, $last->sn);
            $num = $date == $arr[1] ? intval($arr[2]) + 1 : 1;
        } else {
            $num = 1;
        }

        $sn = 'CGD' . $con . $date . $con . str_pad($num, 3, '0', STR_PAD_LEFT);

        while (PurchaseIn::find()->where(['supplier_id' => $supplierId, 'sn' => $sn])->count() != 0) {
            return $this->createSn();
        }

        return $sn;
    }

    /**
     * 审核入库单
     * @return boolean 审核是否成功
     */
    public function audit() {
        if ($this->status == self::STATUS_AUDITED) {
            return false;
        }

        if ($this->supplier_id != Yii::$app->user->identity->supplier_id) {
            return false;
        }

        $this->status = self::STATUS_AUDITED;
        $this->chk_id = Yii::$app->user->identity->id;
        $this->chk_time = time();
        return $this->save();
    }

    /**
     * 统计已入库金额
     */
    public function countStorageIn(){
        
    }
    

}
